<!-- 非SAP客户关联审批 -->
<template>
<div class="eventLevel">
  <CommonBtn label-name="客户关联审批列表" :buttons="buttons" @onButtonClick="onButtonClick">
    <span slot="conditions" v-for="(item,index) of conditions" :key="index" v-if="item.checked && item.label && item.valueName != undefined">{{item.label+":"+item.valueName}}</span>
  </CommonBtn>
  <CommonList v-if="!showEdit" ref="commonList" :buttons="buttons" :columns="columns" :listUrl="$api.API_SMMS_DEALER_LIST" :conditions="conditions" @tableSelect="tableSelect" @dbClick="openItem"></CommonList>
  <CustomerEdit :pageType="pageType"
                ref="commonEdit"
                :disabled="!enableEdit"
                :label="editTitle"
                v-if="showEdit"
                :items="items"
                :originalData="row"
                @editBack="editBack"
                @approvalPass="approvalPass"
                @approvalBack="approvalBack"/>
  <CommonSearch :label="item.label"
                :conditions="item.conditions"
                v-for="(item, index) of searchs"
                :key="index"
                :listUrl="item.listUrl"
                :columns="item.columns"
                :items="item.items"
                :page="item.page"
                :originalData="item.searchData"
                @submit="item.selectItem"
                @searchBack="searchBack"/>
  <CommonExport :out-btn="button" :props="columns" :export_type="export_type"
                @close="close" :export-url="$api.API_SMMSDEALER_EXPORT" :conditions="conditions"></CommonExport>
</div>
</template>

<script>
import http from '@/api/http'
import {Message } from 'element-ui'
import CommonList from "../../common/CommonList"
import CommonBtn from "../../common/CommonBtn";
import CommonSearch from '../../common/CommonSearch'
import CustomerEdit from "./CustomerEdit";
import CommonExport from "../../common/CommonExport";
export default {
  name: 'smmsDealerRelationList',
  data() {
    return {
      buttons: [
        this.$config.BTNS.EXPORT,
        this.$config.BTNS.SEARCH
      ],
      searchData: {},
      searchItems: [],
      searchs: [],
      showEdit: false,
      editTitle: '关联客户审批查看',
      enableEdit: false,
      row: null,
      pageType: 4,
      conditions: [{
        checked: true,
        prop: 'pageType',
        value: '4'
      }],
      columns: [{
          prop: 'smmsCode',
          label: '统一编码'
        },
        {
          prop: 'smmsName',
          label: '客户名称'
        },
        {
          prop: 'levelName',
          label: '客户属性'
        },
        {
          prop: 'dataSource',
          label: '数据来源'
        },
        {
          prop: 'smmsOwnerName',
          label: '供货方名称'
        },
        {
          prop: 'status',
          label: '状态'
        },
        {
          prop: 'buName',
          label: '业务单位'
        },
        {
          prop: 'customNameCn',
          label: '客户集团'
        },
        {
          prop:'isLKAvalue',
          label:'是否LKA'
        },
        {
          prop: 'brandName',
          label: '适用品牌'
        },
        {
          prop: 'province',
          label: '省',
          minWidth: 50
        },
        {
          prop: 'physicalCity',
          label: '市',
          minWidth: 50
        },
        {
          prop: 'adminCity',
          label: '区县',
          minWidth: 50
        },
        {
          prop: 'deAddress',
          label: '详细地址'
        },
        {
          prop: 'repName',
          label: '负责人'
        },
        {
          prop: 'createTime',
          label: '创建时间'
        },
        {
          prop: 'modifyTime',
          label: '最后修改时间'
        }
      ],
      items: [{
          name: '统一编码',
          key: 'smmsCode',
          type: 'text',
          disable: true,
          required: true,
          placeholder: '系统自动生成'
        },
        {
          name: '客户属性',
          key: 'levelName',
          vkey: 'fkDealerLevel',
          type: 'single-select',
          required: true,
          options: []
        },
        {
          name: 'SoldToCode(客户编码)',
          key: 'soldToCode',
          type: 'checktext',
          required: true,
          disable: false,
          placeholder: '',
          checkname: '是否自动生成'
        },
        {
          name: '客户名称',
          key: 'smmsName',
          type: 'text',
          placeholder: '',
          required: true
        },
        {
          name: '地理城市',
          key: 'city',
          type: 'multi-select',
          required: false,
          optionss: [{
            options: [],
            key: 'province'
          }, {
            options: [],
            key: 'physicalCity'
          }, {
            options: [],
            key: 'adminCity'
          }]
        },
        {
          name: '城市类别',
          key: 'cityTypeName',
          vkey: 'fkCityType',
          type: 'single-select',
          required: false,
          disable: true,
          options: []
        },
        {
          name: '详细地址',
          key: 'deAddress',
          type: 'multi-text',
          required: false,
          texts: [{
            key: 'address',
            name: '街道',
            span: 12
          }, {
            key: 'address1',
            name: '路',
            span: 12
          }, {
            key: 'address2',
            name: null,
            span: 24
          }]
        },
        {
          name: '客户负责人',
          key: 'repName',
          type: 'search',
          required: true,
          disable: false
        },
        {
          name: '关联供货方',
          key: 'smmsOwnerName',
          type: 'search',
          required: false,
          disable: false
        },
        {
          name: '供货方代码',
          key: 'smmsSoldToCode',
          type: 'text',
          required: false,
          disable: true
        },
        {
          name: '业务单位',
          key: 'buName',
          vkey: 'fkBu',
          type: 'single-select',
          required: false,
          disable: true,
          options: []
        },
        {
          name: '客户集团',
          key: 'customNameCn',
          type: 'search',
          required: true,
          disable: true
        },
        {
          name: '是否LKA',
          key: 'isLKAvalue',
          vkey: 'isLKA',
          type: 'single-select',
          required: true,
          disable: false,
          placeholder: '请选择',
          options: []
        },
        {
          name: '适用品牌',
          key: 'brandName',
          vkey: 'fkBrand',
          type: 'single-select',
          required: true,
          disable: true,
          options: []
        },
        {
          name: 'Destination',
          key: 'destinationName',
          vkey: 'fkDestination',
          type: 'single-select',
          required: false,
          disable: true,
          options: []
        },
        {
          name: '客户联系人',
          key: 'linkMan',
          type: 'text',
          placeholder: ''
        },
        {
          name: '联系人手机',
          key: 'linkPhone',
          type: 'text',
          placeholder: ''
        },
        {
          name: '邮箱地址',
          key: 'linkEmail',
          type: 'text',
          placeholder: '多个邮箱请用英文,(逗号分割)'
        },
        {
          name: '申请费用状态',
          key: 'tpFeeStatus',
          type: 'single-select',
          disable: false,
          options: []
        },
        {
          name: '客户营业状态',
          key: 'businessStatus',
          type: 'single-select',
          disable: false,
          options: []
        },
        {
          name: '合作状态',
          key: 'cooperationStatus',
          type: 'single-select',
          disable: false,
          options: []
        },
        {
          name: 'TPWF客户编码',
          key: 'tpCode',
          type: 'text',
          required: false,
          disable: true
        },
        {
          name: 'JDES Code',
          key: 'jdesCode',
          type: 'text',
          required: false,
          disable: true
        },
      ],
        export_type:[{
            checked: true,
            prop: 'pageType',
            value: '4'
        }],
        button:null,
    }
  },
  methods: {
    onButtonClick(item) {
      let self = this
      if (item.event == 'search()') {
        this.searchs.push({
          label: '查询客户信息',
          page: 'customer',
          searchData: this.searchData,
          items: this.searchItems,
          selectItem: function (formData, items) {
            self.searchs.pop()
            self.searchItems = items;
            self.searchData = formData;
            let data = {}
            let conditions = self.conditions.filter(item=>{
              return !item.label
            })
            // 回调
            for (let idx in items) {
              let item = items[idx]
              if (item.checked == true) {
                if (item.vkey) {
                  data[item.vkey] = formData[item.vkey]
                } else {
                  data[item.key] = formData[item.key]
                }
                conditions.push({
                  checked: true,
                  label: item.name,
                  prop: item.vkey ? item.vkey : item.key,
                  valueName: formData[item.key],
                  value: item.vkey ? formData[item.vkey] : formData[item.key]
                })
              }
            }
            self.conditions = conditions
          },
        })
      }
      this.button = item
    },
    tableSelect(row) {
      this.row = row
    },
    openItem(item) {
      // 打开详情
      this.showEdit = true
      this.enableEdit = false
    },
    searchBack() {
      this.searchs.pop()
    },
    editBack() {
      this.showEdit = false
    },
    approvalPass(item) {
      http.post(this.$api.API_SMMS_NOSAP_RELATED_APPROVAL_PASS,{pkid: item.pkid})
          .then(resp=>{
            Message({type: 'success', message: '审核通过'})
            this.row = null
            this.showEdit = false
            this.$refs.commonList.getList()
          })
    },
    approvalBack(item, reason) {
      http.post(this.$api.API_SMMS_NOSAP_RELATED_APPROVAL_BACK,{pkid: item.pkid, reason: reason})
          .then(resp=>{
            Message({type: 'success', message: '审核驳回'})
            this.row = null
            this.showEdit = false
            this.$refs.commonList.getList()
          })
    },
      close(item) {
          this.button = item
      }
  },
  watch: {
    showEdit(newVal,oldVal) {
      if (!newVal && oldVal) {
        this.conditions = this.conditions.map(item=>{return item})
      }
    }
  },
  components: {
      CommonExport,
      CommonList,
      CommonBtn,
      CustomerEdit,
      CommonSearch
  }
}
</script>
